package com.cmcc.groupcontacts.firewall.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cmcc.groupcontacts.common.PreferenceManager;
import com.cmcc.groupcontacts.firewall.activity.strategy.ActivityTitleStrategy;
import com.yinhai.activity.R;

/**
 * 拦截设置Activity
 * 
 * @author
 * 
 */
public class FirewallConfigActivity extends BaseActivity {
	private static final String[] FIREWALL_TONES = new String[] { "忙音", "空号",
			"已关机", "已停机" };
	// 占线时转移
	private static final String DISABLE_SERVICE = "tel:%23%2367%23";
	// 占线时转移，提示所拨的号码为空号
	private static final String ENABLE_SERVICE_EMPTY = "tel:**67*13800000000%23";
	// 占线时转移，提示所拨的号码为关机
	private static final String ENABLE_SERVICE_POWEROFF = "tel:**67*13810538911%23";
	// 占线时转移，提示所拨的号码为停机
	private static final String ENABLE_SERVICE_STOP = "tel:**67*13701110216%23";

	@Override
	public void setTitleStrategy() {
		titleStrategy = new ActivityTitleStrategy(this);
	}

	@Override
	public void setTitleButtons() {
		// 左边的“返回”按钮
		getLeftLayout().addView(createBackButton());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firewall_config_activity);

		init();
	}

	private void init() {
		// 智能拦截开启/关闭按钮
		setFirewallSwitcher(PreferenceManager.getFirewallState());
		findViewById(R.id.lay_firewall_switcher).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						boolean isFirewallOpen = !PreferenceManager
								.getFirewallState();
						setFirewallSwitcher(isFirewallOpen);
						PreferenceManager.setFirewallState(isFirewallOpen);
					}
				});
		// 拦截模式切换按钮
		findViewById(R.id.lay_firewall_mode).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(FirewallConfigActivity.this,
								FirewallConfigModeActivity.class));
					}
				});
		// 拦截模式定时切换按钮
		findViewById(R.id.lay_firewall_timing).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO: 拦截模式定时切换
					}
				});
		// 来电拦截提示音按钮
		final TextView txtFirewallTone = (TextView) findViewById(R.id.txt_firewall_tone);
		txtFirewallTone.setText(FIREWALL_TONES[PreferenceManager
				.getFirewallTone()]);
		findViewById(R.id.lay_firewall_tone).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						new AlertDialog.Builder(FirewallConfigActivity.this)
								.setTitle(R.string.config_tone_title)
								.setSingleChoiceItems(FIREWALL_TONES,
										PreferenceManager.getFirewallTone(),
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// 仅支持移动MMI码设置
												switch (which) {
												case 0: // 忙音
													startActivity(new Intent(
															Intent.ACTION_CALL).setData(Uri
															.parse(DISABLE_SERVICE)));
													break;
												case 1: // 空号
													startActivity(new Intent(
															Intent.ACTION_CALL).setData(Uri
															.parse(ENABLE_SERVICE_EMPTY)));
													break;
												case 2: // 已关机
													startActivity(new Intent(
															Intent.ACTION_CALL).setData(Uri
															.parse(ENABLE_SERVICE_POWEROFF)));
													break;
												case 3: // 已停机
													startActivity(new Intent(
															Intent.ACTION_CALL).setData(Uri
															.parse(ENABLE_SERVICE_STOP)));
													break;
												}

												txtFirewallTone
														.setText(FIREWALL_TONES[which]);
												PreferenceManager
														.setFirewallTone(which);
												dialog.dismiss();
											}
										}).show();
					}
				});
		// 拦截关键字按钮
		findViewById(R.id.txt_firewall_keywords).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(FirewallConfigActivity.this,
								FirewallConfigKeywordsActivity.class));
					}
				});
	}

	/*
	 * 设置智能拦截的开启/关闭状态显示
	 */
	private void setFirewallSwitcher(boolean isFirewallOpen) {
		TextView txtFirewallSwitcher = (TextView) findViewById(R.id.txt_firewall_switcher);
		if (isFirewallOpen) {
			Drawable imgOn = getDrawable(R.drawable.ic_switch_open);
			imgOn.setBounds(0, 0, imgOn.getMinimumWidth(),
					imgOn.getMinimumHeight());
			txtFirewallSwitcher.setCompoundDrawables(null, null, imgOn, null);

			setItemState(R.id.lay_firewall_mode, R.id.txt_firewall_mode_title,
					true);
			setItemState(R.id.lay_firewall_timing,
					R.id.txt_firewall_timing_title, true);
			setItemState(R.id.lay_firewall_tone, R.id.txt_firewall_tone_title,
					true);
			setItemState(R.id.txt_firewall_keywords,
					R.id.txt_firewall_keywords, true);
		} else {
			Drawable imgOff = getDrawable(R.drawable.ic_switch_close);
			imgOff.setBounds(0, 0, imgOff.getMinimumWidth(),
					imgOff.getMinimumHeight());
			txtFirewallSwitcher.setCompoundDrawables(null, null, imgOff, null);

			setItemState(R.id.lay_firewall_mode, R.id.txt_firewall_mode_title,
					false);
			setItemState(R.id.lay_firewall_timing,
					R.id.txt_firewall_timing_title, false);
			setItemState(R.id.lay_firewall_tone, R.id.txt_firewall_tone_title,
					false);
			setItemState(R.id.txt_firewall_keywords,
					R.id.txt_firewall_keywords, false);
		}
	}

	/*
	 * 设置启用/禁用状态
	 */
	private void setItemState(int layResId, int txtResId, boolean isEnable) {
		findViewById(layResId).setEnabled(isEnable);
		if (isEnable) {
			((TextView) findViewById(txtResId))
					.setTextColor(getColor(R.color.list_item_name));
		} else {
			((TextView) findViewById(txtResId))
					.setTextColor(getColor(R.color.list_item_phone));
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		setFirewallMode();
	}

	/*
	 * 设置拦截模式
	 */
	private void setFirewallMode() {
		TextView txtFirewallMode = (TextView) findViewById(R.id.txt_firewall_mode);
		switch (PreferenceManager.getFirewallMode()) {
		case 0:
			txtFirewallMode.setText(R.string.config_mode_standard);
			break;
		case 1:
			txtFirewallMode.setText(R.string.config_mode_block_black);
			break;
		case 2:
			txtFirewallMode.setText(R.string.config_mode_pass_white);
			break;
		case 3:
			txtFirewallMode.setText(R.string.config_mode_pass_local_contacts);
			break;
		case 4:
			txtFirewallMode.setText(R.string.config_mode_pass_all_contacts);
			break;
		case 5:
			txtFirewallMode.setText(R.string.config_mode_block_all);
			break;
		case 6:
			txtFirewallMode.setText(R.string.config_mode_customize);
			break;
		}
	}
}
